﻿using AVCUMPlatform.Entities;
using Microsoft.AspNetCore.Mvc;
using AVCUMPlatform.HelperLibrary;
using HelperLibrary;

namespace AVCUMPlatform.Controllers
{
    /// <summary>
    /// 来源接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class UserController : ControllerBase
    {
        private IConfiguration _configuration;
        public UserController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="login">登录信息</param>
        /// <returns></returns>
        [HttpPost]
        [NoNeedLogin]
        public ActionResult Login(LoginReq login)
        {
            var account = _configuration["LoginUser:Account"];
            var password = _configuration["LoginUser:Password"];
            if (login.uid != account || login.pwd != password)
            {
                return new JsonResult(new { code = -1 });
            }
            var token = Util.MD5Encrypt(account + password);
            CacheHelper.Set("login_token", token, TimeSpan.FromHours(12), isSliding: false);
            var name = _configuration["LoginUser:Name"];
            return new JsonResult(new { code = 1, name });
        }

        /// <summary>
        /// 清除登录缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NoNeedLogin]
        public ActionResult ClearLoginCache()
        {
            CacheHelper.Remove("login_token");
            return new JsonResult(new { code = 1 });
        }
    }
}
